/*
 * 赵晓莉
 * */
require(["jquery","template","bootstrap3.min","bbcare"],function($,template,bootstrap,$bb) {
    var URL = $bb.baseUrl("department")+"followup/service";
    var account=eval('('+sessionStorage.account+')');
    var accountName=account.alias;
    $bb.ajax({
        url: URL + "/department/randOutPatientScheduleList",
        type: "post",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        data: JSON.stringify({
            data: {}
        }),
        success: function (data) {
            /*
             * scheduleType:排班类型：1按周排班，2按月排班，3按具体日期排班
             * workDay：工作日：按周排班，则用0-6表示周日到周六；按月排班，则用1-31表示；按日期排班，则用具体日期表示
             * workPeriod：按一天上下午及晚上排班，则用0，1，2分别表示上午、下午和晚上，若用具体时间排班，则用具体时间表示 添加参数
             * */
            for(var i=0;i<data.data.rows.length;i++){
                if(data.data.rows[i].scheduleType==1){
                    var workDay=data.data.rows[i].workDay,  //工作日：
                        workPeriod=data.data.rows[i].workPeriod,//时段
                        scheduleId=data.data.rows[i].id;//排班id
                    var day=work(workDay);
                    $("."+day+workPeriod).addClass("on").attr("id",scheduleId).append('<i class="fa fa-user" aria-hidden="true" style="color:white;"></i>');
                }
            }
        },
        error: function (data) {}
    });

    $("#schedule-time>tr").find("td:gt(0)").click(function(){
        var accountActive=accountName+"修改了排班";
        var workDay;
        if($(this).index()==7){
            workDay=0;
        }else{
            workDay=$(this).index();
        }
        var workPeriod;
        var firstChild=$(this).parent("tr").find("td:eq(0)").attr("class");
        if(firstChild=="morning"){
            workPeriod=0;
        }else if(firstChild=="afternoon"){
            workPeriod=1;
        }else if(firstChild=="evening"){
            workPeriod=2;
        }
        var that=this;
        var attrClass=$(this).attr("class");
        var res=attrClass.indexOf(" on");
        if(res==-1){
            //添加
            $bb.ajax({
                url: URL + "/department/randOutPatientScheduleAdd",
                type: "post",
                contentType: "application/json;charset=UTF-8",
                dataType: "json",
                data: JSON.stringify({
                    "data": {
                        "oprInfo":{
                            "opCode":"2006",
                            "opNote":accountActive,
                            "opType":"3"
                        },
                        "scheduleInfo":{
                            "scheduleType":"1",
                            "workDay":workDay,
                            "workPeriod":workPeriod
                        }
                    }
                }),
                success: function (data) {
                    if(data.success){
                        $(that).addClass("on").attr("id",data.data.scheduleId).append('<i class="fa fa-user" aria-hidden="true" style="color:white;"></i>');
                        $("#change").show();
                        setTimeout(function(){
                            $("#change").hide();
                        },1500);
                    }
                },
                error: function (data) {}
            });
        }else{
            //删除：
            var scheduleId=$(this).attr("id");
            $bb.ajax({
                url: URL + "/department/randOutPatientScheduleDel",
                type: "post",
                contentType: "application/json;charset=UTF-8",
                dataType: "json",
                data: JSON.stringify({
                    "data":{
                        "oprInfo":{
                            "opCode":"2007",
                            "opNote":accountActive,
                            "opType":"3"
                        },
                        "scheduleInfo":{
                            "scheduleId": scheduleId
                        }
                    }
                }),
                success: function (data) {
                   if(data.success){
                       $(that).removeClass("on").removeAttr("id").html("");
                       $("#change").show();
                       setTimeout(function(){
                           $("#change").hide();
                       },1500);
                   }
                },
                error: function (data) {}
            });
        }
    });
    //工作日处理
    function work(workDay){
        switch(workDay){
            case "1":
                workDay="monday";
                break;
            case "2":
                workDay="tuesday";
                break;
            case "3":
                workDay="wednesday";
                break;
            case "4":
                workDay="thursday";
                break;
            case "5":
                workDay="friday";
                break;
            case "6":
                workDay="saturday";
                break;
            default:
                workDay="sunday";
                break;
        }
        return workDay;
    }

});
